package com.chenhao.generator.controller;

import com.chenhao.generator.controller.vo.FileTreeVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件操作
 *
 * @author:chenHao
 * @version:1.0
 * @since:1.0
 * @createTime:2019-10-02
 */
@Controller
public class FileController {

    private int id = 1;

    /**
     * 查找目录下的所有文件树
     *
     * @param fileTargetSrc
     * @return
     */
    @PostMapping(value = "/file/getFileTree", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<FileTreeVO> getFileTree(String fileTargetSrc) {
        List<FileTreeVO> result = new ArrayList<>();
        System.out.println(fileTargetSrc);
        return readFile(fileTargetSrc, result, 0);
    }

    @PostMapping(value = "/file/getCode", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String getCode(@RequestBody String fileTargetSrc) throws IOException {
        File file = new File(fileTargetSrc.replaceAll("\"",""));
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            memStream.write(buffer, 0, len);
        }
        byte[] data = memStream.toByteArray();
        bufferedInputStream.close();
        memStream.close();
        bufferedInputStream.close();
        return new String(data);
    }


    /**
     * 输出给定目录下的文件，包括子目录中的文件
     *
     * @param dirPath 给定的目录
     */
    private List<FileTreeVO> readFile(String dirPath, List<FileTreeVO> result, int pid) {
        // 建立当前目录中文件的File对象
        File file = new File(dirPath);
        if (file.exists()) {
            // 取得代表目录中所有文件的File对象数组
            File[] list = file.listFiles();
            // 遍历file数组
            for (int i = 0; i < list.length; i++) {
                if (list[i].isDirectory()) {
                    FileTreeVO fileTreeVO = new FileTreeVO();
                    fileTreeVO.setId(id++);
                    fileTreeVO.setPid(pid);
                    fileTreeVO.setTitle(list[i].getName());
                    fileTreeVO.setFilePath(list[i].getPath());
                    fileTreeVO.setIsDirectory(true);
                    // 递归子目录
                    List<FileTreeVO> child = new ArrayList<>();
                    readFile(list[i].getPath(), child, fileTreeVO.getId());
                    fileTreeVO.setChildren(child);
                    result.add(fileTreeVO);
                } else {
                    FileTreeVO fileTreeVO = new FileTreeVO();
                    fileTreeVO.setId(id++);
                    fileTreeVO.setPid(pid);
                    fileTreeVO.setTitle(list[i].getName());
                    fileTreeVO.setFilePath(list[i].getPath());
                    fileTreeVO.setIsDirectory(false);
                    result.add(fileTreeVO);
                }
            }
        }
        return result;
    }

}
